// $Id: addressbook.js 18 2009-01-05 02:18:30Z volker.kueffel $

Ext.onReady(function() {

addressFields=new Array('subdn','givenName','sn','cn', 'mozillaNickname', 'mail','mozillaSecondEmail','nsAIMid', 'telephoneNumber', 'homePhone', 'fax', 'pager','mobile'
							,'homeStreet', 'mozillaHomeStreet2', 'mozillaHomeLocalityName', 'mozillaHomeState', 'mozillaHomePostalCode', 'mozillaHomeCountryName','street'
							,'mozillaWorkStreet2', 'l', 'st', 'postalCode', 'c', 'title', 'department:', 'company', 'mozillaWorkUrl', 'mozillaHomeUrl', 'mozillaCustom1', 'mozillaCustom2'
							,'mozillaCustom3', 'mozillaCustom4', 'description');
					
					

inputPanel=new Ext.form.FormPanel({
								labelWidth:120,
								border: false,
					            buttons: [{
					                    text     : 'Save',
					                    disabled : true,
					                    handler  : function(){
					                        inputPanel.getForm().submit({url:'store_contact.php'});
					                    }
					                },{
					                    text     : 'Cancel',
					                    handler  : function(){
					                        editWindow.hide();
					                    }
				                }],
				   	        	items: [{
		   	        				xtype: 'tabpanel',
		   	        				deferredRender: false,
		   	        				bodyStyle:'padding:5px 5px 0',
		   	        				activeTab: 0,
		   	        				border: false,
		   	        				items: [{
		   	        					title: 'Contact',
		   	        					items: [
				        	        		{
				        	        			xtype:'fieldset',
				        	        			title: 'Name',
									            autoHeight:true,
           										defaults: {width: 210},
           										defaultType: 'textfield',
				        	        			items: [
				        	        				{id:'givenname',name:'name',fieldLabel:'First Name'},
				        	        				{id:'sn',name:'sn',fieldLabel:'Last Name'},
				        	        				{id:'cn',name:'cn',fieldLabel:'Display Name'},
				        	        				{id:'mozillaNickname',name:'mozillaNickname',fieldLabel:'Nickname'}
				        	        			]
				        	        		},{
				        	        			xtype:'fieldset',
				        	        			title: 'Internet',
									            autoHeight:true,
           										defaults: {width: 210},
           										defaultType: 'textfield',
				        	        			items: [
				        	        				{id:'mail',name:'mail',fieldLabel:'Email', vtype: 'email'},
				        	        				{id:'mozillaSecondEmail',name:'mozillaSecondEmail',fieldLabel:'Additional Email', vtype: 'email'},
				        	        				{id:'nsAIMid',name:'nsAIMid',fieldLabel:'Messenger Name'},
				        	        			]
				        	        		},{
				        	        			xtype:'fieldset',
				        	        			title: 'Phone',
									            autoHeight:true,
           										defaults: {width: 210},
           										defaultType: 'textfield',
				        	        			items: [
				        	        				{id:'telephoneNumber',name:'telephoneNumber',fieldLabel:'Work Phone'},
				        	        				{id:'homePhone',name:'homePhone',fieldLabel:'Home Phone'},
				        	        				{id:'fax',name:'fax',fieldLabel:'Fax'},
				        	        				{id:'pager',name:'pager',fieldLabel:'Pager'},
				        	        				{id:'mobile',name:'mobile',fieldLabel:'Mobile'}
				        	        			]
				        	        		}
				        	        	]
				        	        	},{
				   	        				title: 'Address',
				   	        				items:[
				   	        					{
					        	        			xtype:'fieldset',
					        	        			title: 'Private',
										            autoHeight:true,
										            labelWidth:100,
            										defaults: {width: 210},
            										defaultType: 'textfield',
					        	        			items: [
					        	        				{id:'homeStreet',name:'homeStreet',fieldLabel:'Address'},
					        	        				{id:'mozillaHomeStreet2',name:'mozillaHomeStreet2',labelSeparator:''},
					        	        				{id:'mozillaHomeLocalityName',name:'mozillaHomeLocalityName',fieldLabel:'City'},
					        	        				{id:'mozillaHomePostalCode',name:'mozillaHomePostalCode',fieldLabel:'Postal Code'},
					        	        				{id:'mozillaHomeState',name:'mozillaHomeState',fieldLabel:'State'},
					        	        				{id:'mozillaHomeCountryName',name:'mozillaHomeCountryName',fieldLabel:'Country'},
					        	        				{id:'mozillaHomeUrl',name:'mozillaHomeUrl',fieldLabel:'Website'}
					        	        			]
				        	        			},{
					        	        			xtype:'fieldset',
					        	        			title: 'Business',
										            autoHeight:true,
										            labelWidth:100,
            										defaults: {width: 210},
            										defaultType: 'textfield',
					        	        			items: [
					        	        				{id:'street',name:'street',fieldLabel:'Address'},
					        	        				{id:'mozillaWorkStreet2',name:'mozillaWorkStreet2',labelSeparator:''},
					        	        				{id:'l',name:'l',fieldLabel:'City'},
					        	        				{id:'postalCode',name:'postalCode',fieldLabel:'Postal Code'},
					        	        				{id:'st',name:'st',fieldLabel:'State'},
					        	        				{id:'c',name:'c',fieldLabel:'Country'},
					        	        				{id:'mozillaWorkUrl',name:'mozillaWorkUrl',fieldLabel:'Website'}
					        	        			]
				        	        			}
				   	        				]
				        	        	},{
				        	        		title: 'Misc',
				        	        		autoHeight:true,
				        	        		defaultType:'textfield',
				        	        		layout: 'form',
				        	        		
				        	        		items: [
					        	        		{id:'mozillaCustom1',name:'mozillaCustom1',fieldLabel:'Miscellaneous 1'},
					        	        		{id:'mozillaCustom2',name:'mozillaCustom2',fieldLabel:'Miscellaneous 2'},
					        	        		{id:'mozillaCustom3',name:'mozillaCustom3',fieldLabel:'Miscellaneous 3'},
					        	        		{id:'mozillaCustom4',name:'mozillaCustom4',fieldLabel:'Miscellaneous 4'},
					        	        		{
					        	        			xtype:'fieldset',
					        	        			title: 'Notes',
					        	        			autoHeight:true,
					        	        			labelWidth: 1,
					        	        			items: [{xtype: 'textarea', id:'description',name:'description',fieldLabel:'', labelSeparator:'',width: 440,height:200}]
				        	        			}
				        	        		]
				        	        	}]
			        	        	}]
					        	    
								});


inputPanel.on({
	actioncomplete: function(form, action){
            if(action.type == 'load'){
            // enable save
            }
        }
});


var editWindow = new Ext.Window({
	layout      : 'fit',
	hidden:true,
	width       : 500,
	height      : 540,
	closeAction :'hide',
	plain       : true,
	items:  inputPanel,
});


var addressStore=new Ext.data.JsonStore({
        root: 'data',
    	fields: ['sn','givenname','mail','subdn','cn'],
       	url: 'get_contacts.php'
    });
   
   
 var root = new Ext.tree.AsyncTreeNode({
                text: 'Address book',
                subdn:'',
                draggable:false, // disable root node dragging
                id:'root',
                allowChildren: true,
                visible: true,
                leaf: false
            });
            
var folderLoader=new Ext.tree.TreeLoader({dataUrl:'get_folders.php'});
            
folderLoader.on("beforeload", function(treeLoader, node) {
        this.baseParams.subdn = node.attributes.subdn;
}, folderLoader);


 var action = new Ext.Action({
        text: 'Show All',
        handler: function(){
            addressStore.load({params:{subdn:'',showall:1}});
            userList.setTitle('All Entries');
        },
    });
        
var tbarText='some text';
var userList= new Ext.grid.GridPanel({
	store: addressStore,
	region: 'center',
	title: '&nbsp;',
    columns: [
        {header: "Last Name", width: 200, sortable: true, dataIndex: 'sn'},
        {header: "First Name", width: 200, sortable: true, dataIndex: 'givenname'},
        {header: "email", width: 120, sortable: true,  dataIndex: 'mail'},
    ],
    viewConfig: {
        forceFit: true
    },
});
userList.on("cellclick",function(grid, rowIndex, columnIndex, e) {
    var record = grid.getStore().getAt(rowIndex); 
    if (record) {
    	editWindow.show();
    	editWindow.setTitle(record.data.cn);
    	for (i=0; i<addressFields.length; i++) {
    		field=inputPanel.findById(addressFields[i]);
    		if (field) {
    			field.reset();
    		}
    	}
    	//this.load({params:{subdn:record.data.subdn}});
    	inputPanel.load({url:'get_contacts.php',params:{subdn:record.data.subdn,full:1,single:1}});
    }
},inputPanel);
        
var folderTree=new Ext.tree.TreePanel({
				region: 'west',
				width:200,
				minWidth:75,
				split:true,
				height: 350,
				border: false,
                animate:true, 
                autoScroll:true,
                root: root,
                rootVisible: true,
                loader: folderLoader,
                enableDD:false,
                containerScroll: true,
 				tbar: [action],                
                //dropConfig: {appendOnly:true}
            });

folderTree.on("click", function(node) {
	this.load({params:{subdn:node.attributes.subdn}});
	userList.setTitle(node.text);
}, addressStore);    


            
var border = new Ext.Panel({
    title: 'Simple LDAP Address Book',
    layout:'border',
    renderTo:'app',
    width:600,
    height: 400,
    items: [
    	folderTree,
    	userList
    ]
});


});
